Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

279
Visualizações
How to fill multiple models with mongodb using "population"

I am trying to display all the data with the "populate" statement. But I only get one "populate" but when I put many it doesn't work. what I want to do is to bring me the data from the "User", "Customer" model as well.

This is my code

My Model:

import { model, Schema } from 'mongoose';

const RoomSchema = new Schema({
  Users: [
    {
      agentId: {
        type: Schema.Types.ObjectId,
        ref: "User",
        required: false,
      },
      customerId: {
        type: Schema.Types.ObjectId,
        ref: "Customer",
        required: false,
      },
      typeId: Number, // 1 - agent, 2 - client
    },
  ],
  Messages: [
    {
      agentId: {
        type: Schema.Types.ObjectId,
        ref: "User",
        required: false,
      },
      customerId: {
        type: Schema.Types.ObjectId,
        ref: "Customer",
        required: false,
      },
      message: {
        type: String,
        required: true,
      },
      date: Date,
      sender: Number,
    },
  ],
  FinishAt: Date,
  FinishBy: String,
  typeFinishBy: Number, // 1 - agent, 2 - client
}, {
    timestamps: true,
    versionKey: false
});

export default model('Room', RoomSchema);

this is the sentence I am using

import Room from "../models/Room.js";

async function getOnlyRoom(id) {

 const foundRoom = await Room.findById(id)
 .populate('Users.agentId')
 .populate('Users.customerId')
 .populate('Messages.agentId')
 .populate('Messages.customerId')
 .execPopulate();

  return foundRoom
}

Image of Json Postman

only works with one

foundRoom.populate('Users.customerId')

Works with only populate

this is the error image of error

Thank you very much for your help

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I have found the solution to my problem I was researching the solution on the mongoose website and did it with a single populate.

 const populateRoom = await foundRoom
.populate([
  { path: 'Users.agentId', select: 'name' },
  { path: 'Users.customerId', select: 'name' },
  { path: 'Messages.agentId', select: 'name' },
  { path: 'Messages.customerId', select: 'name' }
])

return populateRoom;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda